package com.springboot.demo.entity.exception;

import com.springboot.demo.util.enums.Code;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.text.MessageFormat;

/**
 * @Description：
 * @Date： Created in 2021/12/30
 * @author： jianbin.lei
 * @Auto： <a href="mailto:493168771@qq.com">jianbin.lei</a>
 */
@Data
@Accessors(chain=true)
@EqualsAndHashCode(callSuper=false)
public class ResponseException extends RuntimeException {

    private Long code;

    private String msg;

    public ResponseException(){
    }

    public ResponseException(Code errCode){
        this.code = errCode.getCode();
        this.msg = errCode.getMessage();
    }

    public static ResponseException ResponseExceptionFormat(Code errCode, Object ... arguments){
        return new ResponseException(errCode.getCode(), MessageFormat.format(errCode.getMessage(),arguments));
    }


    public ResponseException(Code errCode,String extMsg){
        this.code = errCode.getCode();
        StringBuilder sb = new StringBuilder(errCode.getMessage());
        sb.append(":").append(extMsg);
        this.msg = sb.toString();
    }

    public ResponseException(Long code,String msg){
        this.code = code;
        this.msg = msg;
    }
}
